package front

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"qihuang/middleware"
	"qihuang/protocol"
	"qihuang/service/front"
	"strconv"
)

type Care struct {
}

//文章列表
func (Care) GetList(c *gin.Context) {
	resp := &protocol.Resp{Status: 0, Msg: "", Data: ""}
	checkResult := middleware.TokenCheck(c)
	if !checkResult {
		resp.Status = 1
		resp.Msg = "身份验证失败"
		c.JSON(http.StatusOK, resp)
		return
	}
	familyId, err := strconv.Atoi(c.DefaultQuery("family_id", "-1"))
	if err != nil || familyId == -1 {
		resp.Status = 1
		resp.Msg = "参数错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	page, err := strconv.Atoi(c.DefaultQuery("page", "1"))
	if err != nil {
		resp.Status = 1
		resp.Msg = "参数错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	pageSize, err := strconv.Atoi(c.DefaultQuery("page_size", "20"))
	if err != nil {
		resp.Status = 1
		resp.Msg = "参数错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	//cateId, err := strconv.Atoi(c.DefaultQuery("category_id", "0"))
	//if err != nil {
	//	cateId = 0
	//}
	articleServ := front.Cares{}
	fields := []string{"article_id", "dear_id"}
	resp = articleServ.GetList(familyId, page, pageSize, fields)
	//resp = articleServ.GetListForEs(page, pageSize, cateId, fields)
	//if resp.Status == -999 {
	//	resp = articleServ.GetListForMysql(page, pageSize, cateId, fields)
	//}
	c.JSON(http.StatusOK, resp)
}

//文章详情
func (Care) Show(c *gin.Context) {
	resp := &protocol.Resp{Status: 1, Msg: "", Data: ""}
	checkResult := middleware.TokenCheck(c)
	if !checkResult {
		resp.Status = 2
		resp.Msg = "身份验证失败"
		c.JSON(http.StatusOK, resp)
		return
	}
	id, err := strconv.Atoi(c.Param("id"))
	articleServ := front.Cares{}
	if err != nil || id <= 0 {
		resp.Status = 1
		resp.Msg = "参数错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	resp = articleServ.GetDetail(id)
	c.JSON(http.StatusOK, resp)
}
